package view;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import model.Laege;
import model.Patient;

/**
 * @uthor BWH
 */

public class GUI_PatientOversigt extends JPanel {
	JTable table;
	TeraFootMain teraFootMain;

	/**
	 * Create the panel.
	 */
	public GUI_PatientOversigt(TeraFootMain teraFootMain) {
		this.teraFootMain = teraFootMain;

		setBackground(SystemColor.activeCaption);
		setLayout(new GridLayout(0, 1, 0, 0));

		final JScrollPane scrollPane = new JScrollPane();
		add(scrollPane);

		table = new JTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setBackground(Color.WHITE);
		scrollPane.setViewportView(table);
		refreshTable();
		table.setDefaultRenderer(Object.class,
				new AlternateTableRowColorRenderer());
	}

	public void refreshTable() {
		final ArrayList<Patient> patienter = teraFootMain.getDbConnection()
				.addAllPatients();
		Laege laege;
		final String[][] array = new String[patienter.size()][13];
		int i = 0;
		for (final Patient pat : patienter) {
			array[i][0] = "" + pat.getCpr();
			array[i][1] = "" + pat.getFornavn();
			array[i][2] = "" + pat.getEfternavn();
			array[i][3] = "" + pat.getTelefonNummer();
			array[i][4] = "" + pat.getMobilNumer();
			array[i][5] = "" + pat.getBy();
			array[i][6] = "" + pat.getPostNummer();
			array[i][7] = "" + pat.getAdresse();
			array[i][8] = "" + pat.getEmail();
			array[i][9] = "" + pat.getSygesikring();
			laege = teraFootMain.getDbConnection()
					.getLaege(pat.getLaegeIndex());
			array[i][10] = "" + laege.getNavn();
			array[i][11] = "" + laege.getTelefon();
			array[i][12] = "" + laege.getAdresse();
			i++;
		}
		table.setModel(new DefaultTableModel(array, new String[] { "CPR",
				"Fornavn", "Efternavn", "Telefon", "Mobil tlf", "By",
				"Postnummer", "Adresse", "Email", "Sygesikring", "L\u00E6ge",
				"L\u00E6ges tlf", "L\u00E6ges adresse" }) {
			boolean[] columnEditables = new boolean[] { false, false, false,
					false, false, false, false, false, false, false, false,
					false, false };

			@Override
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});

		System.out.println("Table refreshed");
	}

	public void refreshTable(ArrayList<Patient> patienter) {
		Laege laege;
		final String[][] array = new String[patienter.size()][13];
		int i = 0;
		for (final Patient pat : patienter) {
			array[i][0] = "" + pat.getCpr();
			array[i][1] = "" + pat.getFornavn();
			array[i][2] = "" + pat.getEfternavn();
			array[i][3] = "" + pat.getTelefonNummer();
			array[i][4] = "" + pat.getMobilNumer();
			array[i][5] = "" + pat.getBy();
			array[i][6] = "" + pat.getPostNummer();
			array[i][7] = "" + pat.getAdresse();
			array[i][8] = "" + pat.getEmail();
			array[i][9] = "" + pat.getSygesikring();
			laege = teraFootMain.getDbConnection()
					.getLaege(pat.getLaegeIndex());
			array[i][10] = "" + laege.getNavn();
			array[i][11] = "" + laege.getTelefon();
			array[i][12] = "" + laege.getAdresse();
			i++;
		}
		table.setModel(new DefaultTableModel(array, new String[] { "CPR",
				"Fornavn", "Efternavn", "Telefon", "Mobil tlf", "By",
				"Postnummer", "Adresse", "Email", "Sygesikring", "L\u00E6ge",
				"L\u00E6ges tlf", "L\u00E6ges adresse" }) {
			boolean[] columnEditables = new boolean[] { false, false, false,
					false, false, false, false, false, false, false, false,
					false, false };

			@Override
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});

		System.out.println("Table refreshed");
	}

	class AlternateTableRowColorRenderer extends DefaultTableCellRenderer {

		Color currColor = Color.white;

		public AlternateTableRowColorRenderer() {
			setOpaque(true);
		}

		@Override
		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {

			if (row % 2 == 0) {
				currColor = Color.white;
			} else {
				currColor = new Color(222, 222, 255);
			}
			if (isSelected) {
				super.setForeground(table.getSelectionForeground());
				super.setBackground(table.getSelectionBackground());
			} else {
				super.setForeground(Color.black);
				super.setBackground(currColor);
			}
			setFont(table.getFont());
			setValue(value);

			return this;
		}
	}

	public JTable getTable() {
		return table;
	}

}
